package com.lykj.llw.box.adapter

import android.widget.ImageView
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lykj.lib_base.component.GlideApp
import com.lykj.lib_base.internal.utils.PackageUtils
import com.lykj.llw.box.R
import com.lykj.llw.box.custom.view.CustomCornerProgressBar
import com.lykj.llw.box.data.model.LocalDownloadRecordModel

/**
 * ================================================
 * 下载管理器列表适配器
 * ================================================
 *
 * @author gosha
 * @date 2023/12/4-13:54
 */
class DownloadManagerAdapter :
    BaseQuickAdapter<LocalDownloadRecordModel, BaseViewHolder>(R.layout.item_download_manager) {
    override fun convert(holder: BaseViewHolder, item: LocalDownloadRecordModel) {
        holder.setText(R.id.tv_name, "${item.name}")

        if (item.totalBytes.toInt() != 0) {
            val progress = item.currentBytes / item.totalBytes
            holder.getView<CustomCornerProgressBar>(R.id.pb_download)
                .setProgress((progress * 100).toInt())
        }

        if (item.isManagerModel) {
            holder.getView<ImageView>(R.id.iv_delete).setImageResource(R.mipmap.ic_can_delete)
        } else {
            holder.getView<ImageView>(R.id.iv_delete).setImageResource(R.mipmap.ic_not_can_delete)
        }

        when (item.status) {
            0 -> {
                holder.setText(R.id.tv_btn_option, "下载中")
                holder.setText(R.id.tv_down_status, "下载中")
            }

            1 -> {
                if (PackageUtils.checkApk(context, item.packageName)) {
                    holder.setText(R.id.tv_btn_option, "打开")
                } else {
                    holder.setText(R.id.tv_btn_option, "安装")
                }
                holder.setText(R.id.tv_down_status, "下载完成")
            }

            2 -> {
                holder.setText(R.id.tv_btn_option, "等待下载")
                holder.setText(R.id.tv_down_status, "等待下载")
            }

            -1 -> {
                holder.setText(R.id.tv_btn_option, "继续")
                holder.setText(R.id.tv_down_status, "暂停")
            }
        }

        GlideApp.with(context)
            .load(item.icon)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(holder.getView(R.id.iv_icon))

        holder.setText(
            R.id.tv_size,
            "${"%.1f".format(item.currentBytes / 1024 / 1024)}MB / ${"%.1f".format(item.totalBytes / 1024 / 1024)}MB"
        )
    }
}